

local MarketplaceService = game:GetService("MarketplaceService")
local M = _G.CommonOHOH.class.new(script, script.Parent.base,"mall_handle_"..script.Name);

function M.BuyGamePass(self,passId)
    -- print("购买通行证-->:",passId)
    local hasPass = false
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(self.parent:GetUserId(), passId)
	end)
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return false
	end

	if hasPass then
        return false
	else
		return MarketplaceService:PromptGamePassPurchase(self.parent.main_inst, passId)
	end
end

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent)
end;

function M.dtor(self)
	M.super.dtor(self);
end;

--检查是否能够购买（子类去覆盖）
function M.Check(self,gid,gtype)
	local gamePassData=self.DataBase:GetData("gamePass")
	local cfg=_G.CommonOHOH.config.Goods[gid]
    for k,v in pairs(cfg.itemData) do
       if gamePassData[k] then
			return false
	   end
    end
	return true
end


function M.ClientBuy(self,gid,...)
	return self:BuyGamePass(_G.ClientOHOH.config.Goods[gid].pid)
end

function M.IsMe(self,gid,gtype)
	 local r=script.Name==gtype
    return  r
end


--服务器验证购买完成，处理发放逻辑（子类去覆盖）
function M.ServerBuyDone(self,gid,gtype,...)
	local gamePassData=self.DataBase:GetData("gamePass")
	local cfg=_G.CommonOHOH.config.Goods[gid]
    for k,v in pairs(cfg.itemData) do
		gamePassData[k]=true
    end
	self.parent:SynchDataToClient("gamePass")

	self.DataBase:Execut(function()
	end);
end


return M